package org.csu.newpetstoreback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.newpetstoreback.common.CommonResponse;
import org.csu.newpetstoreback.entity.Collect;
import org.csu.newpetstoreback.entity.Item;
import org.csu.newpetstoreback.persistence.CollectMapper;
import org.csu.newpetstoreback.persistence.ItemMapper;
import org.csu.newpetstoreback.service.CollectService;
import org.csu.newpetstoreback.vo.CollectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("collectService")
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public CommonResponse<List<CollectVO>> getCollects(String username) {
        List<CollectVO> collectVOList = new ArrayList<>();
        //在所有collect中寻找当前用户的所有收藏商品List
            QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username", username);//符合用户名要求
            List<Collect> collectList = collectMapper.selectList(queryWrapper);

            if(collectList.isEmpty()){
                return CommonResponse.createForNotFound(1,"该用户没有收藏商品");
            }

            for (Collect collect:collectList){
                QueryWrapper<Item> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("itemId",collect.getItemId());
                Item item = itemMapper.selectOne(queryWrapper1);

                CollectVO collectVO = entityToCollectVO(collect, item);
                collectVOList.add(collectVO);
            }
        return CommonResponse.createForSuccess(collectVOList);

    }

    @Override
    public CommonResponse<CollectVO> addCollect(String username,String itemId) {
        CommonResponse<CollectVO> response = isCollectIn(username,itemId);
        if(!response.isSuccess()){
            return response;
        }

        int insertCollectSuccess=-1;

        Collect collect = new Collect();
        collect.setItemId(itemId);
        collect.setUsername(username);
        insertCollectSuccess=collectMapper.insert(collect);

        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemId",collect.getItemId());
        Item item = itemMapper.selectOne(queryWrapper);

        if(insertCollectSuccess>0){
            CollectVO collectVO = entityToCollectVO(collect, item);
            return CommonResponse.createForSuccess(collectVO,"加入收藏夹成功");
        }
        else{
            return CommonResponse.createForNotFound(2,"加入收藏夹失败");
        }
    }

    @Override
    public CommonResponse<CollectVO> getCollectById(String username,String itemId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);//符合用户名要求
        queryWrapper.eq("itemId",itemId);//符合收藏商品id
        Collect collect = collectMapper.selectOne(queryWrapper);

        QueryWrapper<Item> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("itemId",itemId);
        Item item = itemMapper.selectOne(queryWrapper1);

        if(collect == null){
            return CommonResponse.createForNotFound(1,"收藏夹中没有该商品");
        }
        CollectVO collectVO = entityToCollectVO(collect, item);
        return CommonResponse.createForSuccess(collectVO);
    }

    @Override
    public CommonResponse<CollectVO> deleteCollectById(String username,String itemId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);//符合用户名要求
        queryWrapper.eq("itemId",itemId);//符合收藏商品id
        int deleteCollectSuccess = collectMapper.delete(queryWrapper);

        if(deleteCollectSuccess > 0){
            return CommonResponse.createForSuccessMessage("商品取消收藏成功");

        }else {
            return CommonResponse.createForNotFound(1,"没有该收藏商品，无法删除");
        }
    }

    @Override
    public CommonResponse<CollectVO> isCollectIn(String username, String itemId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);//符合用户名要求
        queryWrapper.eq("itemId",itemId);//符合收藏商品id
        Collect collect = collectMapper.selectOne(queryWrapper);
        if(collect == null){
            return CommonResponse.createForSuccessMessage("商品尚未添加到收藏夹");
        }else{
            return CommonResponse.createForNotFound(3,"收藏夹中已有该商品，请勿重复添加");
        }

    }

    private CollectVO entityToCollectVO(Collect collect, Item item){
        CollectVO collectVO = new CollectVO();
        collectVO.setUsername(collect.getUsername());
        collectVO.setItemId(collect.getItemId());

        collectVO.setIImage(item.getIImage());
        collectVO.setListPrice(item.getListPrice());
        collectVO.setProductId(item.getProductId());
        collectVO.setCategoryId(item.getCategoryId());

        return collectVO;
    }
}
